package com.cch.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


//声明配置类

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();
        // 跨域设置
        corsConfiguration.addAllowedHeader("*");  //配置那些请求头可以跨域访问
        corsConfiguration.addAllowedMethod("*"); //配置那些方法可以跨域访问
        corsConfiguration.addAllowedOrigin("*"); //配置那些来源可以跨域访问

        corsConfiguration.setAllowCredentials(true); //配置跨域请求可以携带cookie
        corsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);

        return new CorsWebFilter(corsConfigurationSource);
    }
}